//
//  SSFirstPageDiscountTitleView.m
//  Travel_World
//
//  Created by dlios on 15/10/16.
//  Copyright © 2015年 TLS. All rights reserved.
//

#import "SSFirstPageDiscountTitleView.h"
#import "SSFirstPageDiscountTitleModel.h"


@interface SSFirstPageDiscountTitleView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIButton *styleButton;
@property (nonatomic, strong) UIButton *startButton;
@property (nonatomic, strong) UIButton *endButton;
@property (nonatomic, strong) UIButton *timeButton;
@property (nonatomic, strong) NSMutableArray *styleArr;
@property (nonatomic, strong) NSMutableArray *departureArr;
@property (nonatomic, strong) NSMutableArray *poiArr;
@property (nonatomic, strong) NSMutableArray *timesArr;
@property (nonatomic, strong) NSMutableArray *countryArr;
@property (nonatomic, assign) SSTitleButton ButtonSytle;
@property (nonatomic, copy) NSString *styleStr;
@property (nonatomic, copy) NSString *startStr;
@property (nonatomic, copy) NSString *endStr;
@property (nonatomic, copy) NSString *timeStr;
@property (nonatomic, assign) BOOL isOpen;

@end

@implementation SSFirstPageDiscountTitleView

- (instancetype)initWithFrame:(CGRect)frame delegate:(id<SSFirstPageDiscountTitleViewDelegate>)SSFirstPageDiscountTitleViewDelegate
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _delegate = SSFirstPageDiscountTitleViewDelegate;
        _styleStr = @"";
        _startStr = @"";
        _endStr = @"";
        _timeStr = @"";
        
        [self createButton];
        _isOpen = NO;
    }
    return self;
}


- (void)createButton
{
    self.styleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.startButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.endButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.timeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.styleButton.frame = CGRectMake(0, 3, self.frame.size.width / 4, 30);
    self.styleButton.font = [UIFont systemFontOfSize:15];
    self.startButton.frame = CGRectMake(self.frame.size.width / 4, 3, self.frame.size.width / 4, 30);
    self.startButton.font = [UIFont systemFontOfSize:15];
    self.endButton.frame = CGRectMake(self.frame.size.width / 2, 3, self.frame.size.width / 4, 30);
    self.endButton.font = [UIFont systemFontOfSize:15];
    self.timeButton.frame = CGRectMake(self.frame.size.width * 0.75, 3, self.frame.size.width / 4, 30);
    self.timeButton.font = [UIFont systemFontOfSize:15];
    [self.styleButton setTitle:@"全部类型" forState:UIControlStateNormal];
    [self.startButton setTitle:@"出发地" forState:UIControlStateNormal];
    [self.endButton setTitle:@"目的地" forState:UIControlStateNormal];
    [self.timeButton setTitle:@"旅行时间" forState:UIControlStateNormal];
    
    
    
    [self.styleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.startButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.endButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.timeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [self.styleButton addTarget:self action:@selector(styleButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.startButton addTarget:self action:@selector(startButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.endButton addTarget:self action:@selector(endButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.timeButton addTarget:self action:@selector(timeButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:self.styleButton];
    [self addSubview:self.startButton];
    [self addSubview:self.endButton];
    [self addSubview:self.timeButton];
    
}


- (UITableView *)secondaryView
{
    if (!_secondaryView) {
        _secondaryView = [[UITableView alloc] initWithFrame:CGRectMake(WIDTH / 2, 35, WIDTH / 2, HEIGHT - 35) style:UITableViewStylePlain];
        _secondaryView.rowHeight = 35;
        _secondaryView.delegate = self;
        _secondaryView.dataSource = self;
    }
    
    if (!_secondaryView.superview) {
        [self addSubview:_secondaryView];
    }
    
    return _secondaryView;
}

- (UITableView *)mainTableView
{
    if (!_mainTableView) {
        
        _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 35, WIDTH, HEIGHT - 35) style:UITableViewStylePlain];
        _mainTableView.rowHeight = 40;
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
    }
    
    if (!_mainTableView.superview) {
        [self addSubview:_mainTableView];
    }
    
    return _mainTableView;

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (self.isOpen) {
        
        if (tableView == self.mainTableView) {
            
            if (self.ButtonSytle == style) {
                return self.styleArr.count;
            } else if (self.ButtonSytle == departure) {
                return self.departureArr.count;
            } else if (self.ButtonSytle == times_drange) {
                return self.timesArr.count;
            } else{
                return self.poiArr.count;
            }
        } else {
            return self.countryArr.count;
        }
    } else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.mainTableView == tableView) {
        
        static NSString *main = @"main";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:main];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:main];
            cell.textLabel.font = [UIFont systemFontOfSize:14];
        }
        
        if (self.ButtonSytle == style) {
            cell.textLabel.text = [self.styleArr[indexPath.row] catename];
        }
        
        if (self.ButtonSytle == departure) {
            cell.textLabel.text = [self.departureArr[indexPath.row] city_des];
        }
        
        if (self.ButtonSytle == times_drange) {
            cell.textLabel.text = [self.timesArr[indexPath.row] mydescription];
        }
        
        if (self.ButtonSytle == poi) {
            
            cell.textLabel.text = [self.poiArr[indexPath.row] continent_name];
        }
        
        return cell;
        
    } else {
        
        static NSString *secondary = @"secondary";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:secondary];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:secondary];
            cell.backgroundColor = [UIColor lightGrayColor];
            cell.textLabel.font = [UIFont systemFontOfSize:12];
        }
        
        cell.textLabel.text = self.countryArr[indexPath.row][@"country_name"];
        
        return cell;
    }
}


- (void)createArrByUrl:(NSString *)url;
{
    if (self.ButtonSytle == style) {
        [QTAFNnetWorkTool getUrl:url body:nil hudSuperView:self result:QTJSON headerFile:nil success:^(id result) {
            
            self.styleArr = [SSFirstPageDiscountTitleModel baseModelByArr:result[@"data"][@"type"]];
            
            [_mainTableView reloadData];
            
        }];
    }
    
    if (self.ButtonSytle == departure) {
        [QTAFNnetWorkTool getUrl:url body:nil hudSuperView:self result:QTJSON headerFile:nil success:^(id result) {
            
            self.departureArr = [SSFirstPageDiscountTitleModel baseModelByArr:result[@"data"][@"departure"]];
            
            [_mainTableView reloadData];
            
        }];
    }
    
    if (self.ButtonSytle == times_drange) {
        [QTAFNnetWorkTool getUrl:url body:nil hudSuperView:self result:QTJSON headerFile:nil success:^(id result) {
            
            self.timesArr = [SSFirstPageDiscountTitleModel baseModelByArr:result[@"data"][@"times_drange"]];
            
            [_mainTableView reloadData];
            
        }];
    }
    
    if (self.ButtonSytle == poi) {
        [QTAFNnetWorkTool getUrl:url body:nil hudSuperView:self result:QTJSON headerFile:nil success:^(id result) {
            
            self.poiArr = [SSFirstPageDiscountTitleModel baseModelByArr:result[@"data"][@"poi"]];
            
            [_mainTableView reloadData];
            
        }];
    }
}

- (void)styleButtonClick
{
    self.ButtonSytle = style;

    if (!self.isOpen) {
        self.mainTableView.backgroundColor = [UIColor whiteColor];
        self.isOpen = YES;
        
        if (!(self.styleArr.count > 0)) {
            
            [self createArrByUrl:[_delegate urlForm]];
        } else {
            [_mainTableView reloadData];
        }
        
    } else {
        
        self.isOpen = NO;
        [self removeSuperView];
    }
}

- (void)startButtonClick
{
    
    self.ButtonSytle = departure;
    
    if (!self.isOpen) {
        self.mainTableView.backgroundColor = [UIColor whiteColor];
        self.isOpen = YES;
        
        if (!(self.departureArr.count > 0)) {
            
            [self createArrByUrl:[_delegate urlForm]];
            
        } else {
            
            [_mainTableView reloadData];
        }
        
    } else {
        
        self.isOpen = NO;
        [self removeSuperView];
    }
}

- (void)endButtonClick
{
    self.ButtonSytle = poi;

    if (!self.isOpen) {
        self.mainTableView.backgroundColor = [UIColor whiteColor];
        self.isOpen = YES;
        
        if (!(self.poiArr.count > 0)) {
            
            [self createArrByUrl:[_delegate urlForm]];
            
        } else {
            
            [_mainTableView reloadData];
        }

        
    } else {
        
        self.isOpen = NO;
        [self removeSuperView];
    }
}

- (void)timeButtonClick
{
    self.ButtonSytle = times_drange;
    
    if (!self.isOpen) {
        self.mainTableView.backgroundColor = [UIColor whiteColor];
        self.isOpen = YES;
        
        if (!(self.timesArr.count > 0)) {
            
            [self createArrByUrl:[_delegate urlForm]];
            
        } else {
            
            [_mainTableView reloadData];
        }
        
        
    } else {
        
        self.isOpen = NO;
        [self removeSuperView];
        
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.mainTableView == tableView) {
        
        if (self.ButtonSytle == poi) {
            
            self.countryArr = [self.poiArr[indexPath.row] country];
            self.secondaryView.backgroundColor = [UIColor whiteColor];
            [_secondaryView reloadData];
            
        } else {
            self.isOpen = NO;

            [self removeSuperView];
            [self changTitleButton:indexPath.row];
        }
        
    } else {
        self.isOpen = NO;

        [self removeSuperView];
        [self changTitleButton:indexPath.row];
        
    }
}


- (void)changTitleButton:(NSInteger)index
{
    switch (self.ButtonSytle) {
        case style:
            [self.styleButton setTitle:[self.styleArr[index] catename] forState:UIControlStateNormal];
            self.styleStr = [self.styleArr[index] myId].description;
            break;
        case departure:
            [self.startButton setTitle:[self.departureArr[index] city_des] forState:UIControlStateNormal];
            self.startStr = [self.departureArr[index] city];
            break;
        case times_drange:
            [self.timeButton setTitle:[self.timesArr[index] mydescription] forState:UIControlStateNormal];
            self.timeStr = [self.timesArr[index] times];
            break;
        case poi:
            [self.endButton setTitle:self.countryArr[index][@"country_name"] forState:UIControlStateNormal];
            self.endStr = self.countryArr[index][@"country_id"];
            break;
            
        default:
            break;
    }
    
    NSString *strUrl = [NSString stringWithFormat:@"http://open.qyer.com/lastminute/app_lastminute_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&continent_id=0&count=20&country_id=%@&departure=%@&is_show_pay=1&is_show_supplier=1&lat=38.8826676005059&lon=121.5397836874259&max_id=0&oauth_token=3ccf6be2c77dee4d33e6d6a5eb878fc7&page=1&page_size=20&product_type=%@&times=%@", self.endStr, self.startStr, self.styleStr, self.timeStr];
    
    if ([_delegate respondsToSelector:@selector(createData:)]) {
        
        [_delegate createData:strUrl];
    }
}

- (void)removeSuperView
{
    if (_mainTableView.subviews) {
        [_mainTableView reloadData];
        [_mainTableView removeFromSuperview];
    }
    
    if (_secondaryView) {
        [_secondaryView reloadData];
        [_secondaryView removeFromSuperview];
    }
}


/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
